package com.drop.action;

import com.drop.common.MyCart;
import com.drop.common.Util;
import com.drop.entity.Cart;
import com.drop.service.CartService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by 陈东一 on 2017/9/23 17:44
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/cart")
@Scope("prototype")
public class CartAction extends ActionSupport{
	private Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private CartService cartService;

	/**
	 * 展示购物车
	 * @return
	 * @throws Exception
	 */
	@Action(value = "list",results = {
			@Result(name="shopcart",location = "/WEB-INF/home/shopcart.jsp")
	})
	public String showCarts() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int uid = (int) request.getSession().getAttribute("id");
		List<Cart> carts = cartService.showCarts(uid);
		log.info(carts);
		ActionContext.getContext().put("carts",carts);
		return "shopcart";
	}

	/**
	 * 添加购物车
	 * @return
	 * @throws Exception
	 */
	@Action(value = "update",results = {})
	public String updateCartNum() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int uid = (int) request.getSession().getAttribute("id");
		String id = request.getParameter("itemid");
		String num = request.getParameter("num");
		log.info(id+"数量"+num);
		cartService.addCart(uid,Integer.parseInt(id),Integer.parseInt(num));
		Util.returnJson(response,new MyCart("true",num));
		return null;
	}

	/**
	 * 修改购物车商品数量
	 * @return
	 * @throws Exception
	 */
	@Action(value = "add",results = {})
	public String addCart() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int uid = (int) request.getSession().getAttribute("id");
		String id = request.getParameter("itemid");
		String num = request.getParameter("num");
		log.info(id+"数量"+num);
		cartService.addCart(uid,Integer.parseInt(id),Integer.parseInt(num));
		Util.returnJson(response,new MyCart("true",num));
		return null;
	}

	/**
	 * 删除购物车商品
	 * @return
	 * @throws Exception
	 */
	@Action(value = "del",results = {})
	public String delCart() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int uid = (int) request.getSession().getAttribute("id");
		String id = request.getParameter("itemid");
		cartService.deleteCart(uid, Integer.parseInt(id));
		Util.returnJson(response,new MyCart("true","true"));
		return null;
	}
}
